package com.think.admin.web.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.admin.model.mainCompany.MainCompanyDTO;
import com.think.admin.model.mainCompany.MainCompanyQuery;
import com.think.admin.web.mapper.MainCompanyMapper;
import com.think.admin.web.service.IMainCompanyService;
import com.think.common.enums.mainProfit.balanceTypeEnum;
import com.think.common.exception.SysException;
import com.think.common.utils.ResultUtil;
import com.think.common.utils.SysUtils;
import com.think.common.web.service.impl.SuperServiceImpl;
import com.think.dao.entity.MainCompany;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 公司信息表 服务实现类
 * </p>
 *
 * @author cwy
 * @since 2019-07-09
 */
@Service
@Transactional(rollbackFor = SysException.class)
@RequiredArgsConstructor
public class MainCompanyServiceImpl extends SuperServiceImpl<MainCompanyMapper, MainCompany> implements IMainCompanyService {

    /**
     * <p>
     * 分页查询公司信息
     * </p>
     *
     * @author CaiWenYi
     * @date 2020-02-11 10:17:26
     */
    @Override
    public HttpEntity pageMainCompany(Page<MainCompanyDTO> page, MainCompanyQuery mainCompanyQuery) {
        return ResultUtil.success(page.setRecords(baseMapper.selectMainCompany(page, mainCompanyQuery)));
    }

    /**
     * <p>
     * 新增或者修改
     * </p>
     *
     * @author CaiWenYi
     * @date 2020-02-11 10:17:51
     */
    @Override
    public HttpEntity createOrUpdate(MainCompany mainCompany) {

        if(SysUtils.isEmpty(mainCompany.getId())){
            mainCompany.setCreateTime(LocalDateTime.now());
        }

        mainCompany.setUpdateTime(LocalDateTime.now());

        // 添加
        boolean saveIsSuccess = this.saveOrUpdate(mainCompany);
        if (saveIsSuccess) {
            return ResultUtil.success();
        } else if (SysUtils.isNotEmpty(mainCompany.getId())) {
            return ResultUtil.updateError();
        } else {
            return ResultUtil.createError();
        }
    }

    /**
     * <p>
     * 批量删除
     * </p>
     *
     * @author CaiWenYi
     * @date 2020-02-11 10:17:59
     */
    @Override
    public HttpEntity delete(List<String> idList) {
        // 非空判断
        if (SysUtils.isEmpty(idList)) {
            return ResultUtil.requestParamError();
        }

        // 批量删除
        boolean deleteSuccess = this.lambdaUpdate().in(MainCompany::getId, idList).remove();
        if (!deleteSuccess) {
            return ResultUtil.deleteError();
        }
        // 返回
        return ResultUtil.deleteSuccess();
    }
}
